//
//  TNCostAmountTableViewController.m
//  TravelNotes
//
//  Created by 黄锋 on 15/8/29.
//  Copyright (c) 2015年 王浩宇. All rights reserved.
//

#import "TNCostAmountTableViewController.h"
#import "TNCostAmountTableViewCell.h"
#import "TNAccountModel.h"
#import "Macro.h"

@interface TNCostAmountTableViewController ()

@property(nonatomic, strong) NSArray *consumeArr;
@property(nonatomic, strong) NSArray *imageArr;
@property(nonatomic, strong) NSMutableArray *countArr;

@end

@implementation TNCostAmountTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _imageArr = @[@"More", @"CategoryIcon3",@"CategoryIcon7", @"CategoryIcon6", @"CategoryIcon4", @"NodeIconShopping25nb", @"PlanShowIcon10"];
    [self.tableView registerClass:[TNCostAmountTableViewCell class] forCellReuseIdentifier:@"TNCostAmountTableViewCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)setDataArr:(NSArray *)dataArr
{
    _dataArr = dataArr;
    _consumeArr = @[@"其它", @"交通", @"餐饮", @"住宿", @"门票", @"购物", @"娱乐"];
    _countArr = [NSMutableArray array];
    for (NSString *consume in _consumeArr) {
        NSMutableArray *unitArr = [NSMutableArray array];
        NSMutableArray *moneyArr = [NSMutableArray array];
        for (TNAccountModel *model in _dataArr) {
            if ([consume isEqualToString:model.consume]) {
                NSInteger index = [unitArr indexOfObject:model.unit];
                if (index == NSNotFound) {
                    [unitArr addObject:model.unit];
                    [moneyArr addObject:model.money];
                } else {
                    NSString *str = [NSString stringWithFormat:@"%.2f", [moneyArr[index] floatValue] + [model.money floatValue]];
                    [moneyArr replaceObjectAtIndex:index withObject:str];
                }
            }
        }
        NSMutableString *string = [NSMutableString string];
        if (unitArr.count) {
            for (int i = 0; i < unitArr.count; i++) {
                [string appendString:[NSString stringWithFormat:@",%@%@", moneyArr[i], unitArr[i]]];
            }
            [string deleteCharactersInRange:NSMakeRange(0, 1)];
        } else {
            string = [@"0.00元" mutableCopy];
        }
        [_countArr addObject:string];
    }
//    NSLog(@"_countArr:%@", _countArr);
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - tableViewDataSourse

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _countArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (SCREEN_HEIGHT - 150) / 7;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TNCostAmountTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TNCostAmountTableViewCell" forIndexPath:indexPath];
    cell.pic.image = [UIImage imageNamed:_imageArr[indexPath.row]];
    cell.consume.text = _consumeArr[indexPath.row];
    cell.count.text = _countArr[indexPath.row];
    return cell;
}


@end
